package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.converter.SectionConverter;
import com.eschool.beans.datamodels.StudentDataModel;
import com.eschool.dao.StudentDao;
import com.eschool.model.Section;
import com.eschool.model.Student;
import com.eschool.service.StudentService;
import com.eschool.service.impl.StudentServiceImpl;
import com.eschool.util.UserUtil;

@ManagedBean(name="promoteStudentBean")
@ViewScoped
public class PromoteStudentBean {
	
	private Student[] selectedStudentsList;
    private StudentDataModel studentModel;
    private SectionConverter converter;
	StudentService service;	
	private List<Student> students;	
	private List<Section> sections;	
	private Section newClass;
	
	public PromoteStudentBean() {
		service = new StudentServiceImpl();
		sections = new ArrayList<Section>();
		converter = new SectionConverter();
		sections = converter.getSectionDB();
		students = new ArrayList<Student>();
		students = service.getAllStudents();		
		studentModel = new StudentDataModel(students);
	}
		
	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}
	public List<Section> getSections() {
		return sections;
	}
	public void setSections(List<Section> sections) {
		this.sections = sections;
	}

	public StudentDataModel getStudentModel() {
		return studentModel;
	}
	public void setStudentModel(StudentDataModel studentModel) {
		this.studentModel = studentModel;
	}
	public Student[] getSelectedStudentsList() {
		return selectedStudentsList;
	}

	public void setSelectedStudentsList(Student[] selectedStudentsList) {
		this.selectedStudentsList = selectedStudentsList;
	}
	
    public String perform(){
    	
    	List selectedStudents = Arrays.asList(selectedStudentsList);
    	System.out.println(newClass.getSectionId() + newClass.getSectionTitle() + newClass.getClass().getName());
    	if(service.promoteStudent(selectedStudents, newClass))
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info Message", "Student/s Promoted"));
    	else
        	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR,"Error Message", "Student/s not Promoted"));
    	
    	return "PromoteStudent";
    }
	public Section getNewClass() {
		return newClass;
	}
	public void setNewClass(Section newClass) {
		this.newClass = newClass;
	}
}
